package com.qf.Servlet;

import com.qf.Service.SysRoleService;
import com.qf.Service.impl.SysRoleServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.DoctorPoint;
import com.qf.entity.PatientHistory;
import com.qf.entity.SysRole;
import com.qf.entity.SysUserRoleDept;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/sysRoleServlet")
public class SysRoleServlet extends BaseServlet {
    private SysRoleService sysRoleService = new SysRoleServiceImpl();

    //查
    public void selectName(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println(1);
        String name = req.getParameter("name");
        PrintWriter pw = resp.getWriter();
        ResultVo<SysUserRoleDept> resultVo = new ResultVo<>();
        SysUserRoleDept sysUserRoleDept = sysRoleService.selectName(name);
        System.out.println(sysUserRoleDept);
        if (sysUserRoleDept != null) {
            resultVo.setData(sysUserRoleDept);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    增
    public void addSysRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SysRole sysRole = new SysRole();

        sysRole.setId(Integer.parseInt(req.getParameter("id")));
        sysRole.setNum(Long.parseLong(req.getParameter("num")));
        sysRole.setPid(Long.parseLong(req.getParameter("Pid")));
        sysRole.setName(req.getParameter("name"));
        sysRole.setDeptid(Long.parseLong(req.getParameter("deptid")));
        sysRole.setTips(req.getParameter("tips"));
        sysRole.setVersion(Long.parseLong(req.getParameter("version")));
        int num = sysRoleService.addSysRole(sysRole);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    改
    public void updateSysRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SysRole sysRole = new SysRole();
        sysRole.setId(Integer.parseInt(req.getParameter("id")));
        sysRole.setNum(Long.parseLong(req.getParameter("num")));
        sysRole.setPid(Long.parseLong(req.getParameter("Pid")));
        sysRole.setName(req.getParameter("name"));
        sysRole.setDeptid(Long.parseLong(req.getParameter("deptid")));
        sysRole.setTips(req.getParameter("tips"));
        sysRole.setVersion(Long.parseLong(req.getParameter("version")));
        int num = sysRoleService.updateSysRole(sysRole);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

    //    删
    public void deleteSysRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SysRole sysRole = new SysRole();
        String id = req.getParameter("id");
        int num = sysRoleService.deleteSysRole(id);
        PrintWriter pw = resp.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num > 0) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);

        }
        JsonUtils.showJson(pw, resultVo);
    }

}
